package com.mde.placeholder;

import ru.yandex.yandexmapkit.MapController;
import ru.yandex.yandexmapkit.MapView;
import ru.yandex.yandexmapkit.map.MapEvent;
import ru.yandex.yandexmapkit.map.OnMapListener;
import ru.yandex.yandexmapkit.overlay.balloon.BalloonItem;
import ru.yandex.yandexmapkit.utils.GeoPoint;
import ru.yandex.yandexmapkit.utils.ScreenPoint;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MapActivity extends Activity
{
	private MapView mapView;
	
	private MapController map;
	
	private GeoPoint ericpol = new GeoPoint(52.089159,23.698327);
	
	OnMapListener listener;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
        
        mapView = (MapView)findViewById(R.id.map);
        map = mapView.getMapController();
        
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        if (name != null) {
        	GeoPoint geoPoint = new GeoPoint(intent.getDoubleExtra("lat", 0), 
        		intent.getDoubleExtra("lon", 0));
        	map.setPositionAnimationTo(geoPoint);
            
            BalloonItem balloon = new BalloonItem(this, geoPoint);
            balloon.setText(name);
            
            map.showBalloon(balloon);
        } else {
        	map.setPositionAnimationTo(ericpol);
        }
        
        map.setZoomCurrent(15.5F);
        
        mapView.showBuiltInScreenButtons(true);
        
        map.showFindMeButton(false);
        
        initListener();            
    }    
    
	private void initListener() {
		listener = new OnMapListener() {
			@Override
			public void onMapActionEvent(MapEvent e) {
				if (e.getMsg() == MapEvent.MSG_LONG_PRESS) {
		
					GeoPoint point = map.getGeoPoint(new ScreenPoint(e.getX(), e.getY()));
					
				    Intent intent = new Intent();
				    intent.putExtra("lat", Double.toString(point.getLat()));
				    intent.putExtra("lon", Double.toString(point.getLon()));
				    setResult(RESULT_OK, intent);
					
					finish();
				}
			}
		};
		map.addMapListener(listener);
	}
	
	@Override 
	public void finish() {
		mapView = null;
		map = null;
		listener = null;
		super.finish();		
	}
}